<template>
  <div class="container">
    <div class="left">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>套餐管理</h3>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入搜索内容" v-model="queryInfo.value"
              @keyup.enter.native="getAllMeal" clearable>
              <el-button slot="append" icon="el-icon-search"
                @click="getAllMeal"></el-button>

            </el-input>

          </el-col>
          <el-col :span="6">
            <ProductAdd @getAllMeal='getAllMeal' />

          </el-col>

        </el-row>

        <!-- 主体 -->
        <el-table :data="productList" style="width: 100%" border>
          <!-- id -->
          <el-table-column prop="_id" label="套餐编号">
          </el-table-column>
          <!-- 名称 -->
          <el-table-column prop="name" label="名称">
            <template width="190" slot-scope="scope">
              <el-input type="text" v-model="changeData.name"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <!-- 月 -->
          <el-table-column prop="name" label="时长">
            <template width="190" slot-scope="scope">
              <el-input type="text" v-model="changeData.mouth"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.mouth }}</span>
            </template>
          </el-table-column>
          <!-- 价格 -->
          <el-table-column prop="salePrice" label="价格">
            <template width="190" slot-scope="scope">
              <el-input type="text" v-model="changeData.salePrice"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.salePrice }}</span>
            </template>
          </el-table-column>
          <!-- 打折价格 -->
          <el-table-column prop="normalPrice" label="原价">
            <template width="190" slot-scope="scope">
              <el-input type="text" v-model="changeData.normalPrice"
                v-show="scope.row.isEdit" />
              <span
                v-show="!scope.row.isEdit">{{ scope.row.normalPrice }}</span>
            </template>
          </el-table-column>
          <!-- 是否推荐 -->
          <el-table-column prop="normalPrice" label="是否推荐">
            <template width="90" slot-scope="scope">
              <el-switch v-model="type" v-show="scope.row.isEdit">
              </el-switch>
              <span
                v-show="!scope.row.isEdit">{{ scope.row.type?'是': '否'}}</span>
            </template>
          </el-table-column>

          <!-- 操作 -->
          <el-table-column prop="gender" label="操作" width="200">
            <template slot-scope="scope">
              <!-- 删除 -->
              <el-button size="mini" type="danger" icon="el-icon-delete"
                @click="delMeal(scope.row._id)">删除</el-button>
              <!-- 修改 -->
              <el-button size="mini" type="primary" v-show="!scope.row.isEdit"
                icon="el-icon-edit" @click="change(scope.row._id)">
                修改
              </el-button>
              <!-- 保存 -->
              <el-button size="mini" type="success" v-show="scope.row.isEdit"
                @click="upChange"><i
                  class="el-icon-upload el-icon--right"></i>保存</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pages">
          <el-pagination @size-change="" @current-change=""
            :current-page="queryInfo.currentPage" :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>

  </div>
</template>

<script>
import ProductAdd from "./add/ProductAdd.vue";
export default {
  components: { ProductAdd },
  data() {
    return {
      productList: [],
      changeData: { type: '' },
      changeNum: 0,
      queryInfo: { pageSize: 5, currentPage: 1, value: "" },
      total: 0,
      type: true
    };
  },
  watch: {
    "changeData.type"(a, b) {
      console.log(a, b);
      if (a == 0) {
        this.type = false
      }
      if (a == 1)
        this.type = true
    },
    type(a, b) {
      console.log(a, b);
      if (a) {
        this.changeData.type = 1
      } else {
        this.changeData.type = 0

      }

    }
  },
  methods: {
    // 获取
    async getAllMeal() {
      // 发请求
      let { data } = await this.$api.productApi.getAllMeal({
      });
      let arr = data.data
      arr.forEach((v) => {
        v.isEdit = false;
      });
      this.productList = arr
      console.log(data);

    },
    //删除
    delMeal(_id) {
      this.$api.productApi.delMeal({ _id }).then(({ data }) => {
        console.log(data);
        if (data.code == 405) {
          this.$message.error(data.error);
          return
        }
        // 删除提示
        this.$message.success(data.msg);
        this.getAllMeal();

      });
    },
    // 修改
    change(id) {
      if (this.changeNum) return this.$message.error("请先完成当前修改");
      this.changeNum = 1;
      let res = this.productList.filter((v) => v._id == id)[0];
      res.isEdit = true;
      this.changeData = { ...res };
      console.log(this.changeData);
    },
    // 确认修改
    upChange() {
      this.$api.productApi
        .updateMeal(this.changeData)
        .then(({ data }) => {
          console.log(data);
          this.$message.success(data.meta.msg);
          this.getAllMeal();
          this.changeNum = 0;
        });
    },
  },
  created() {
    this.getAllMeal();
  }
};
</script>

<style lang="scss" scoped>
img {
  width: 50px;
  height: 50px;
}

input {
  width: 100px;
}
.container {
  display: flex;
  .left {
    width: 100%;
  }
}
</style>
